package com.service.scheduling.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.dao.enums.MemberJAType;
import com.service.dao.mapper.MemberStoreMapper;
import com.service.dao.model.Member;
import com.service.dao.model.MemberStore;
import com.service.scheduling.dto.MemberStoreChange;
import com.service.scheduling.dto.StoreReleaseCommonParam;
import com.service.scheduling.service.MemberStoreReleaseService;
import com.service.scheduling.service.MemberStoreService;
import com.service.scheduling.utils.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
public class MemberStoreReleaseServiceImpl extends ServiceImpl<MemberStoreMapper, MemberStore> implements MemberStoreReleaseService {

    @Autowired
    private MemberStoreService storeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void releaseMemberStore(Member member, StoreReleaseCommonParam param, MemberJAType releaseType, BigDecimal releaseNum, String remark) {

        BigDecimal targetNum = MathUtil.div(releaseNum.multiply(param.getExchangeRate()), param.getTargetCoinPrice(), 8);

        MemberStoreChange releaseStoreChange = MemberStoreChange.builder()
                .changeNum(releaseNum)
                .coinsId(param.getReleaseCoin().getCode())
                .coinsName(param.getReleaseCoin().getName())
                .jaType(releaseType)
                .memberId(member.getId())
                .remark(remark + "扣除")
                .build();

        Integer jaId = storeService.changeMemberStore(releaseStoreChange);

        MemberJAType productType = getProductType(releaseType);
        MemberStoreChange targetStoreChange = MemberStoreChange.builder()
                .memberId(member.getId())
                .jaType(productType)
                .coinsId(param.getTargetCoin().getCode())
                .coinsName(param.getTargetCoin().getName())
                .changeNum(targetNum)
                .jaId(jaId)
                .remark(remark + "产出")
                .build();

        storeService.changeMemberStore(targetStoreChange);

        log.info("会员<id:{},loginAccount:{}>于{}释放{}{}{}:账户释放数量:{},产生数量:{}", member.getId(),
                member.getUserAccount(), param.getCurrentDateStr(), param.getReleaseCoin().getName(), productType.getMsg(),
                param.getTargetCoin().getName(), releaseNum, targetNum);
    }

    private MemberJAType getProductType(MemberJAType releaseType) {
        switch (releaseType) {
            case RELEASE_BY_RECOMMEND_11:
                return MemberJAType.RELEASE_PRODUCT_BY_RECOMMEND_11;
            case RELEASE_BY_RECOMMEND_12:
                return MemberJAType.RELEASE_PRODUCT_BY_RECOMMEND_12;
            case RELEASE_BY_TEAM:
                return MemberJAType.RELEASE_PRODUCT_BY_TEAM;
            default:
                return MemberJAType.RELEASE_PRODUCT_BY_SELF;
        }
    }


}
